package b2.scene.fight.skills
{
	import b2.scene.fight.FightScene;
	import b2.util.Profile;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	public class SkillArea
	{
		private var x:int;
		private var y:int;
		private var direction:int;
		private var areas:Array;
		
		public function SkillArea(x:int, y:int, direction:int = 0)
		{
			this.direction = direction;
			this.x = x;
			this.y = y;
			areas = new Array();
		}
		
		//添加一个区域 
		public function addArea(front:int, back:int, left:int, right:int):void
		{
			var rect:Rectangle;
			if (direction == 0)
			{
				rect = upArea(front, back, left, right);
			}
			else if (direction == 1)
			{
				rect = rightArea(front, back, left, right);
			}
			else if (direction == 2)
			{
				rect = downArea(front, back, left, right);
			}
			else if (direction == 3)
			{
				rect = leftArea(front, back, left, right);
			}
			areas.push(rect);
			//debug 画出区域		
			if (Profile.DEBUG == 1)
				draw();	
		}
		
		//产生十字区域
		public function addCrossArea(front:int, back:int, left:int, right:int):void
		{
			areas.push(upArea(front,back,left,right));
			areas.push(downArea(front,back,left,right));
			areas.push(leftArea(front,back,left,right));
			areas.push(rightArea(front,back,left,right));
			//debug 画出区域
			if (Profile.DEBUG == 1)
				draw(10);		
		}
		
		private function upArea(front:int, back:int, left:int, right:int):Rectangle
		{
			return new Rectangle(x - left, y - front, right + left, front + back);
		}
		
		private function downArea(front:int, back:int, left:int, right:int):Rectangle
		{
			return new Rectangle(x - right, y - back, right + left, front + back);
		}
		
		private function leftArea(front:int, back:int, left:int, right:int):Rectangle
		{
			return new Rectangle(x - front, y - right, front + back, right + left);
		}
		
		private function rightArea(front:int, back:int, left:int, right:int):Rectangle
		{
			return new Rectangle(x - back, y - left, front + back, right + left);
		}
		
		//矩形之间的碰撞
		public function intersects(rect:Rectangle):Boolean
		{
			for (var i:int = 0; i < areas.length; i++)
			{
				if ((areas[i] as Rectangle).intersects(rect))
					return true;
			}
			return false;
		}
		
		public function contain(x:int, y:int):Boolean
		{
			for (var i:int = 0; i < areas.length; i++)
			{
				if ((areas[i] as Rectangle).contains(x, y))
					return true;
			}
			return false;
		}
		
		//debug 画出区域	
		private function draw(time:int = 15):void	
		{
			return;
			for (var i:int = 0; i < areas.length; i++)		
			{			
				var rect:Rectangle = (areas[i] as Rectangle);				
				var s:Sprite = new Sprite();				
				s.graphics.lineStyle(1, i * 1000);				
				s.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);				
				FightScene.getInstance().effectTopLayer.addChild(s);				
				TweenLite.delayedCall(time,erase,[s]);			
			}		
		}
							
		private function erase(a:Sprite):void		
		{				
			if (a.parent != null)				
				a.parent.removeChild(a);		
		}	
	}
					
}